Skip to content

feat(nuxt): Capture errors caught by <NuxtErrorBoundary> during SSR - #26815

Open
pranav514 wants to merge 4 commits into
getsentry:developfrom
pranav514:feat/nuxt-ssr-error-boundary
Open

pranav514 wants to merge 4 commits into
getsentry:developfrom
pranav514:feat/nuxt-ssr-error-boundary

Conversation

@pranav514

Copy link
Copy Markdown

What

Captures errors caught by <NuxtErrorBoundary> during SSR in Nuxt 4.7+ and reports them to Sentry.

Why

In Nuxt 4.7+, <NuxtErrorBoundary> supports SSR and catches component errors to render its #error slot with a 200 HTTP response. These errors never reached Sentry because Nitro's error hook only fires for unhandled server crashes and the client plugin does not receive vue:error during hydration. We buffer server-side vue:error events and report them on app:rendered via reportNuxtError, while ignoring intentional 3xx/4xx control-flow errors and fatal page crashes already reported by Nitro.

Closes: #26795

@github-actions github-actions Bot added the external PR from an external contributor label Nov 24, 2026
Nuxt 4.7 added SSR support to <NuxtErrorBoundary>, rendering its #error slot
and returning HTTP 200 when a component errors during SSR. These errors were
never forwarded to Sentry because Nitro's error hook only catches uncaught errors,
and the client plugin does not receive vue:error during hydration.
This registers a server-side Nuxt app plugin that listens to vue:error on the
server and captures caught errors during app:rendered, unless the page errored
fatally (nuxtApp.payload.error).
@pranav514
pranav514 force-pushed the feat/nuxt-ssr-error-boundary branch from 66b8cea to ecc2714 Compare November 24, 2026 12:56
@pranav514

Copy link
Copy Markdown
Author

@JPeer264 @chargome @nicohrubec I've implemented a fix for this by adding a server-only plugin (error-boundary.server.ts) that listens to vue:error during SSR and flushes caught errors on app:rendered using reportNuxtError.

It skips reporting if nuxtApp.payload.error is present (so fatal page errors continue to be handled by Nitro without duplication) and filters out 3xx/4xx control-flow errors. Unit test coverage has been added. Please do review it

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

Avoid deferring via setTimeout so events are captured before Cloudflare
or serverless request wrappers dispose the client. Calls flushIfServerless
on app:rendered to ensure pending envelopes are dispatched.
@nicohrubec

Copy link
Copy Markdown
Member

@pranav514 can you please stop pinging random people from the team. we see that you opened a PR and we'll get to it.

@pranav514

Copy link
Copy Markdown
Author

@pranav514 can you please stop pinging random people from the team. we see that you opened a PR and we'll get to it.

Sure I'll take care

Comment on lines +14 to +21
function isIgnoredNuxtError(error: unknown): boolean {
if (!isNuxtError(error)) {
return false;
}
// oxlint-disable-next-line typescript/no-deprecated
const statusCode = error?.status || error?.statusCode;
return !!statusCode && statusCode >= 300 && statusCode < 500;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function already exists in sentry.client.ts and can be reused. We can put this function into an utils file so it's shared and then re-use it from there.

return !!statusCode && statusCode >= 300 && statusCode < 500;
}

export default defineNuxtPlugin(nuxtApp => {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This plugin is re-writing a lot of code that already exists. By reusing the things we already have, this can be condensed to just this:

export default defineNuxtPlugin(nuxtApp => {
  const caught: Parameters<typeof reportNuxtError>[0][] = [];
  nuxtApp.hook('vue:error', (error, instance, info) => {
    if (!isIgnoredNuxtError(error)) caught.push({ error, instance, info });
  });
  // The Nitro `error` hook reports errors that end on the error page.
  nuxtApp.hook('app:rendered', () => {
    if (!nuxtApp.payload.error) caught.forEach(reportNuxtError);
  });
});

Comment thread packages/nuxt/src/module.ts Outdated
addPlugin({ src: moduleDirResolver.resolve('./runtime/plugins/route-detector-legacy.server'), mode: 'server' });
}

addPlugin({ src: moduleDirResolver.resolve('./runtime/plugins/error-boundary.server'), mode: 'server' });

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we add this plugin only when nuxt.options.experimental.ssrErrorBoundary is on?

Otherwise, the hooks just run without doing anything.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd replace the unit tests with one E2E case in nuxt-5: a component that throws during SSR inside <NuxtErrorBoundary>, with a check that the page returns 200 and the error reaches Sentry. These mocks can't show that Nuxt actually calls vue:error.

- Share isIgnoredNuxtError in runtime utils for both client and server plugins
- Condense error-boundary.server.ts to reuse reportNuxtError
- Register plugin only when experimental.ssrErrorBoundary is enabled
- Replace unit tests with an E2E test in nuxt-5 test application
@pranav514

Copy link
Copy Markdown
Author

@s1gr1d Addressed all feedback:

Extracted isIgnoredNuxtError to shared runtime utils and reused it in both sentry.client.ts and error-boundary.server.ts.
Condensed error-boundary.server.ts to reuse reportNuxtError.
Guarded plugin registration so it only registers when nuxt.options.experimental.ssrErrorBoundary is enabled.
Removed the mocked unit tests and added an E2E test in nuxt-5 testing an SSR component error inside with an HTTP 200 response.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit baa820c. Configure here.


const error = await errorPromise;

expect(error.transaction).toEqual('/ssr-error-boundary');

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
expect(error.transaction).toEqual('/ssr-error-boundary');
expect(error.transaction).toEqual('GET /ssr-error-boundary');

could be that this is with GET as this is the server-side.

Comment thread packages/nuxt/src/module.ts Outdated
}

// oxlint-disable-next-line typescript/no-unsafe-member-access
if (nuxt.options.experimental && (nuxt.options.experimental as Record<string, unknown>).ssrErrorBoundary) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (nuxt.options.experimental && (nuxt.options.experimental as Record<string, unknown>).ssrErrorBoundary) {
if ((nuxt.options.experimental as { ssrErrorBoundary?: boolean }).ssrErrorBoundary) {

Comment on lines +6 to +8
experimental: {
ssrErrorBoundary: true,
},

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is on by default, so we can remove this flag.

- Use inline type for experimental.ssrErrorBoundary check
- Remove redundant experimental flag from Nuxt 5 test app config
- Expect HTTP method in server error transaction name
- Assert error fallback visibility instead of matching sanitized error text
@pranav514

Copy link
Copy Markdown
Author

@s1gr1d Thanks for the review! Applied all suggested updates:

Switched to the inline type check (nuxt.options.experimental as { ssrErrorBoundary?: boolean }).ssrErrorBoundary in module.ts.
Removed the redundant experimental.ssrErrorBoundary flag from the nuxt-5 test app config.
Updated the expected transaction name to GET /ssr-error-boundary.
Updated the test assertion to await expect(page.locator('#error-fallback')).toBeVisible()

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

external PR from an external contributor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Nuxt: report errors caught by <NuxtErrorBoundary> during SSR

3 participants